package com.swjd.sbm.controller;

import com.swjd.sbm.service.TouGaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Slf4j
public class TouGaoController {
    @Autowired
    private TouGaoService touGaoService;
    @RequestMapping(value = "/upimg",method= RequestMethod.POST )
    @ResponseBody
    public Map<String,Object> upload(MultipartFile file, HttpServletRequest request){
        System.out.println("ada");
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        //文件的扩展名
        String originalFilename = file.getOriginalFilename();

        String extName=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        String res = sdf.format(new Date());
        // 新文件名
        String newFileName = "sliver" + res + extName;
        // upload文件夹位置,webapp下
        String rootPath = request.getSession().getServletContext().getRealPath("upload/");
        //获取协议名+域名+端口号+项目名 = http+"://"+localhost+":"+8080+/booksystem
        String url= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();

        try{
            // 创建年月文件夹
            Calendar date = Calendar.getInstance();
            File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH)+1));
            // 新文件
            File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
            // 判断目标文件所在目录是否存在
            if( !newFile.getParentFile().exists()) {
                // 如果目标文件所在的目录不存在，则创建父目录
                newFile.getParentFile().mkdirs();
            }
            // 将内存中的数据写入磁盘
            file.transferTo(newFile);
            // 完整的url
            String fileUrl = url+"/upload/"+ date.get(Calendar.YEAR) + "/" + (date.get(Calendar.MONTH)+1) + "/" + newFileName;
            System.out.println(fileUrl);
            map.put("statusCode",200);
            data.put("code",0);
            data.put("msg","上传");
            data.put("imageurl",fileUrl);
            map.put("data",data);
            System.out.println(map);  System.out.println(map.get("data"));
            return  map;
        }catch(Exception e){
            e.printStackTrace();
            return  map;
        }

    }

    @RequestMapping("tougao")
    public Map<String,Object> settoutougao(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String,String> reqbody){
        String text = reqbody.get("text");
        String image = reqbody.get("image");
        String token = reqbody.get("token");
        String shebei = reqbody.get("shebei");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        int count =touGaoService.addp(image,text,date);
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        data.put("msg","发帖成功");
        data.put("code",0);
        map.put("statusCode",200);
        return  map;
    }

    @RequestMapping("mytougao")
    public Map<String,Object> mytougao(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String,String> reqbody){
        String page = reqbody.get("page");
        String pagesize = reqbody.get("pagesize");
        String order = reqbody.get("order");
        String token = reqbody.get("token");
        System.out.println(order+token);

        int p = Integer.parseInt(page);
        int s = Integer.parseInt(pagesize);
        String s1 = "1";
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> list = touGaoService.mygao((p-1)*s,p*s,s1);
        map.put("data",list);
        Map<String,Object> data = new HashMap<>();
        data.put("msg","发帖成功");
        data.put("code",0);
        map.put("statusCode",200);
        return  map;
    }
}
